﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace FileMerge.Models
{
    /// <summary>
    /// 配置
    /// </summary>
    public class Configuration
    {
        //文件编码
        private Encoding encoding = System.Text.Encoding.UTF8;

        /// <summary>
        /// 配置路径
        /// </summary>
        public string Path { get; set; } = string.Empty;

        /// <summary>
        /// 文件目录
        /// </summary>
        public string BaseDirectory { get; set; } = ".";

        /// <summary>
        /// 文件查找样式
        /// </summary>
        public string SearchPattern { get; set; } = "*.sql";

        /// <summary>
        /// 是否查找子目录
        /// </summary>
        public SearchOption SearchOption { get; set; } = SearchOption.AllDirectories;

        /// <summary>
        /// 忽略文件查找样式
        /// </summary>
        public List<string> Ignored { get; set; } = new List<string>();

        /// <summary>
        /// 排序条件
        /// </summary>
        public List<Order> Orders { get; set; } = new List<Order>();

        /// <summary>
        /// 按文件名手工排序
        /// </summary>
        public List<string> ManuallyOrder { get; set; } = new List<string>();

        /// <summary>
        /// 仅合并手工排序文件
        /// </summary>
        public bool OnlyManually { get; set; } = false;

        /// <summary>
        /// 文件编码
        /// </summary>
        public string Encoding
        {
            get { return encoding.EncodingName; }
            set
            {
                try { encoding = System.Text.Encoding.GetEncoding(value); }

                catch (Exception) { throw new Exception("Invalid encoding in configuration."); }
            }
        }

        /// <summary>
        /// 输出文件头
        /// </summary>
        public string Header { get; set; } = "-- merge sample {{DateTime}}\r\n";

        /// <summary>
        /// 合并文件概述
        /// </summary>
        public string Summary { get; set; } = "-- {{Num(D2)}} {{FileName}}";

        /// <summary>
        /// 文件头
        /// </summary>
        public string FileHeader { get; set; } = "\r\n---------------------------------------- [{{FileName}}] [{{FileLastWriteTime}}] ----------------------------------------\r\n";

        /// <summary>
        /// 文件尾
        /// </summary>
        public string FileFooter { get; set; } = string.Empty;

        /// <summary>
        /// 输出文件尾
        /// </summary>
        public string Footer { get; set; } = string.Empty;

        /// <summary>
        /// 输出文件目录
        /// </summary>
        public string Output { get; set; } = $"{Directory.GetCurrentDirectory()}\\file_merge_reslut";

        /// <summary>
        /// 是否允许覆盖输出文件
        /// </summary>
        public bool Overwrite { get; set; } = false;

        /// <summary>
        /// 获取编码
        /// </summary>
        /// <returns></returns>
        public Encoding GetEncoding()
        {
            return encoding;
        }
    }
}
